// ReaderDashboard.java - 读者专属页面
package com.library.gui;

import com.library.entity.*;
import com.library.service.AuthService;
import com.library.service.LibraryDataService;
import com.library.service.ReaderSelfService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ReaderDashboard extends JFrame {
    private ReaderUser currentUser;
    private LibraryDataService dataService;
    private JTabbedPane tabbedPane;
    private ReaderSelfService selfService;

    public ReaderDashboard(ReaderUser user) {
        this.currentUser = user;
        this.dataService = new LibraryDataService();
        this.selfService = new ReaderSelfService(dataService, new AuthService());
        initializeUI();
        loadSampleData();
    }

    private void initializeUI() {
        setTitle("读者自助服务系统 - " + currentUser.getReader().getName());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 600);
        setLocationRelativeTo(null);
        ReaderSelfServicePanel mainPanel = new ReaderSelfServicePanel(currentUser, dataService, selfService);
        add(mainPanel);        

        // 创建顶部信息栏
        JPanel infoPanel = new JPanel(new BorderLayout());
        infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
        infoPanel.setBackground(new Color(240, 245, 255));

        JLabel welcomeLabel = new JLabel("欢迎, " + currentUser.getReader().getName() + "!");
        welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        welcomeLabel.setForeground(new Color(0, 102, 204));

        JLabel readerInfoLabel = new JLabel(String.format(
            "读者ID: %s | 类型: %s | 部门: %s", 
            currentUser.getReader().getReaderId(),
            currentUser.getReader().getType(),
            currentUser.getReader().getDepartment()
        ));
        
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton logoutButton = new JButton("退出登录");
        logoutButton.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(this, 
                "确定要退出登录吗?", "确认退出", JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                this.dispose();
                new LoginFrame().setVisible(true);
            }
        });
        bottomPanel.add(logoutButton);
        add(bottomPanel, BorderLayout.SOUTH);

        infoPanel.add(welcomeLabel, BorderLayout.WEST);
        infoPanel.add(readerInfoLabel, BorderLayout.CENTER);
        infoPanel.add(logoutButton, BorderLayout.EAST);

        // 创建选项卡
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("我的借阅", createMyBorrowPanel());
        tabbedPane.addTab("图书查询", createBookSearchPanel());
        tabbedPane.addTab("我的罚金", createFinePanel());
        tabbedPane.addTab("个人信息", createProfilePanel());

        add(infoPanel, BorderLayout.NORTH);
        add(tabbedPane, BorderLayout.CENTER);
    }

    private JPanel createMyBorrowPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 借阅信息表格
        String[] columns = {"图书名称", "作者", "借阅日期", "应还日期", "状态", "续借次数"};
        DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
        JTable borrowTable = new JTable(tableModel);

        // 添加示例数据
        tableModel.addRow(new Object[]{"Java核心技术", "凯·S.霍斯特曼", "2024-01-15", "2024-02-15", "借阅中", "0"});
        tableModel.addRow(new Object[]{"深入理解计算机系统", "Randal E.Bryant", "2024-01-10", "2024-02-10", "借阅中", "1"});

        JScrollPane scrollPane = new JScrollPane(borrowTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        // 操作按钮
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton renewButton = new JButton("续借选中");
        JButton returnButton = new JButton("归还选中");

        renewButton.addActionListener(e -> {
            int selectedRow = borrowTable.getSelectedRow();
            if (selectedRow >= 0) {
                JOptionPane.showMessageDialog(this, "续借成功!");
            } else {
                JOptionPane.showMessageDialog(this, "请选择要续借的图书!");
            }
        });

        returnButton.addActionListener(e -> {
            int selectedRow = borrowTable.getSelectedRow();
            if (selectedRow >= 0) {
                JOptionPane.showMessageDialog(this, "归还成功!");
            } else {
                JOptionPane.showMessageDialog(this, "请选择要归还的图书!");
            }
        });

        buttonPanel.add(renewButton);
        buttonPanel.add(returnButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        return panel;
    }

    private JPanel createBookSearchPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 搜索栏
        JPanel searchPanel = new JPanel(new FlowLayout());
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        JComboBox<String> searchType = new JComboBox<>(new String[]{"书名", "作者", "ISBN"});

        searchPanel.add(new JLabel("搜索:"));
        searchPanel.add(searchType);
        searchPanel.add(searchField);
        searchPanel.add(searchButton);

        // 图书表格
        String[] columns = {"书名", "作者", "出版社", "出版日期", "可借复本", "状态", "操作"};
        DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
        JTable bookTable = new JTable(tableModel);

        // 加载图书数据
        for (Book book : dataService.getAllBooks()) {
            tableModel.addRow(new Object[]{
                book.getTitle(), book.getAuthor(), book.getPublisher(),
                book.getPublishDate(), book.getAvailableCopies(),
                book.getStatus(), "借阅"
            });
        }

        JScrollPane scrollPane = new JScrollPane(bookTable);
        panel.add(searchPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        // 搜索功能
        searchButton.addActionListener(e -> {
            String keyword = searchField.getText().trim();
            if (!keyword.isEmpty()) {
                tableModel.setRowCount(0);
                for (Book book : dataService.searchBooks(keyword)) {
                    tableModel.addRow(new Object[]{
                        book.getTitle(), book.getAuthor(), book.getPublisher(),
                        book.getPublishDate(), book.getAvailableCopies(),
                        book.getStatus(), "借阅"
                    });
                }
            }
        });

        return panel;
    }

    private JPanel createFinePanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 罚金信息
        JPanel fineInfoPanel = new JPanel(new GridLayout(3, 2, 10, 10));
        fineInfoPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        fineInfoPanel.add(new JLabel("当前欠款:"));
        fineInfoPanel.add(new JLabel("0.00 元", JLabel.RIGHT));

        fineInfoPanel.add(new JLabel("超期图书:"));
        fineInfoPanel.add(new JLabel("0 本", JLabel.RIGHT));

        fineInfoPanel.add(new JLabel("最后更新:"));
        fineInfoPanel.add(new JLabel("2024-01-20", JLabel.RIGHT));

        // 罚金明细表格
        String[] columns = {"产生日期", "事由", "金额", "状态"};
        DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
        JTable fineTable = new JTable(tableModel);

        // 添加示例数据
        tableModel.addRow(new Object[]{"2024-01-18", "图书超期", "5.00", "未缴纳"});
        tableModel.addRow(new Object[]{"2024-01-05", "图书超期", "3.50", "已缴纳"});

        JScrollPane scrollPane = new JScrollPane(fineTable);

        // 操作按钮
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton payButton = new JButton("缴纳罚金");
        payButton.addActionListener(e -> {
            JOptionPane.showMessageDialog(this, "缴费功能开发中...");
        });

        buttonPanel.add(payButton);

        panel.add(fineInfoPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        return panel;
    }

    private JPanel createProfilePanel() {
        JPanel panel = new JPanel(new GridLayout(8, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        Reader reader = currentUser.getReader();

        panel.add(new JLabel("读者ID:"));
        panel.add(new JLabel(reader.getReaderId()));

        panel.add(new JLabel("姓名:"));
        panel.add(new JLabel(reader.getName()));

        panel.add(new JLabel("性别:"));
        panel.add(new JLabel(reader.getGender()));

        panel.add(new JLabel("电话:"));
        panel.add(new JLabel(reader.getPhone()));

        panel.add(new JLabel("邮箱:"));
        panel.add(new JLabel(reader.getEmail()));

        panel.add(new JLabel("部门:"));
        panel.add(new JLabel(reader.getDepartment()));

        panel.add(new JLabel("读者类型:"));
        panel.add(new JLabel(reader.getType().toString()));

        panel.add(new JLabel("账户状态:"));
        panel.add(new JLabel(reader.getStatus().toString()));

        return panel;
    }

    private void loadSampleData() {
        // 这里可以加载读者的真实数据
    }
}
